package com.woniuxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.ConstantData;
import com.woniuxy.controller.form.CommentSearchForm;
import com.woniuxy.controller.form.StoreReplayForm;
import com.woniuxy.dao.CommentAttachmentMapper;
import com.woniuxy.dao.StoreInfoMapper;
import com.woniuxy.entity.CommentAttachment;
import com.woniuxy.entity.CommentInfo;
import com.woniuxy.dao.CommentInfoMapper;
import com.woniuxy.entity.StoreInfo;
import com.woniuxy.entity.User;
import com.woniuxy.exception.comment.CommentInfoException;
import com.woniuxy.exception.comment.CommentInfoExceptionCode;
import com.woniuxy.service.CommentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.UserService;
import com.woniuxy.service.dto.CommentCountDto;
import com.woniuxy.service.dto.CommentDto;
import com.woniuxy.service.dto.WoniuPageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评价信息表 服务实现类
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */
@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements CommentInfoService {

    @Autowired
    private CommentInfoMapper mapper;
    @Autowired
    private StoreInfoMapper storeInfoMapper;
    @Autowired
    private CommentAttachmentMapper commentAttachmentMapper;
    @Autowired
    private UserService userService;

    @Override
    public void replay(StoreReplayForm form, Integer accountId) {
        StoreInfo storeInfo = storeInfoMapper.selectOne(new QueryWrapper<StoreInfo>().eq("accountId", accountId));
        CommentInfo commentInfo = mapper.selectById(form.getId());
        if(commentInfo==null || commentInfo.getDeleteFlag()== ConstantData.STATE_DEL){
            throw new CommentInfoException(CommentInfoExceptionCode.COMMENT_IS_NOT_EXIST);
        }
        if(commentInfo.getStoreId()!=storeInfo.getId()){
            throw new CommentInfoException(CommentInfoExceptionCode.STORE_NO_ACCESS_COMMENT);
        }
        commentInfo.setReplay(form.getReplay());
        commentInfo.setReplayFlag(ConstantData.IS_REPLAY);
        this.saveOrUpdate(commentInfo);
    }

    @Override
    public void delReplay(Long id, Integer accountId) {
        CommentInfo commentInfo = mapper.selectById(id);
        if(commentInfo==null){
            throw new CommentInfoException(CommentInfoExceptionCode.COMMENT_IS_NOT_EXIST);
        }
        if(commentInfo.getReplayFlag()==ConstantData.NOT_REPLAY){
            throw new CommentInfoException(CommentInfoExceptionCode.REPLAY_NOT_EXIST);
        }
        commentInfo.setReplay("");
        commentInfo.setReplayFlag(ConstantData.NOT_REPLAY);
        this.saveOrUpdate(commentInfo);
    }

    @Override
    public WoniuPageDto searchForStore(CommentSearchForm form, Integer accountId) {
        StoreInfo storeInfo = storeInfoMapper.selectOne(new QueryWrapper<StoreInfo>().eq("accountId", accountId));
        Page<CommentInfo> page = new Page<>(form.getCurrent() == null ? 1 : form.getCurrent(), form.getSize() == null ? 3 : form.getSize());
        mapper.selectPage(page,new QueryWrapper<CommentInfo>().eq("storeId", storeInfo.getId()).eq("deleteFlag",ConstantData.STATE_NORMAL));
        List<CommentDto> dtos = new ArrayList<>();
        for (CommentInfo commentInfo : page.getRecords()) {
            CommentDto commentDto = BeanUtil.toBean(commentInfo, CommentDto.class);
            List<CommentAttachment> pictures = commentAttachmentMapper.selectList(new QueryWrapper<CommentAttachment>().eq("commentId", commentInfo.getId()));
            List<String> ulrs = new ArrayList<>();
            for (CommentAttachment commentAttachment : pictures) {
                ulrs.add(commentAttachment.getFilePath());
            }
            commentDto.setPictures(ulrs);
            User user = userService.getById(commentInfo.getUserId());
            if(commentInfo.getStoreScore()==5){
                commentDto.setScoreResult("非常满意");
            }else if(commentInfo.getStoreScore()==4){
                commentDto.setScoreResult("满意");
            }else if(commentInfo.getStoreScore()==3){
                commentDto.setScoreResult("一般");
            }else{
                commentDto.setScoreResult("差评");
            }
            commentDto.setUserPhoto(user.getUserImg());
            commentDto.setUserName(user.getUserName());
            dtos.add(commentDto);
            commentDto.computeAverage();
        }
        WoniuPageDto woniuPageDto = BeanUtil.toBean(page, WoniuPageDto.class);
        woniuPageDto.setList(dtos);
        return woniuPageDto;
    }

    @Override
    public CommentCountDto countComment(Integer uid) {
        StoreInfo storeInfo = storeInfoMapper
                .selectOne(new QueryWrapper<StoreInfo>().lambda().eq(StoreInfo::getAccountId, uid));
        Integer lowScore = baseMapper.selectLowScore(storeInfo.getId());
        Integer notReplied = baseMapper.selectNotReplied(storeInfo.getId());
        return new CommentCountDto(lowScore, notReplied);
    }
}
